from __future__ import division
import sys,os,time,datetime
from src.Equipments.LockIn import SR830
from src.Equipments.RFGenerator import E8257D
from src.Equipments.multimeter import AgilentMulti
from src.Equipments.gaussmeter import Lakeshore455
from src.Equipments.EDMR_magnet import EDMRmagnet

from src.Data.DataStructure import Data

import numpy as np
import matplotlib.pyplot as plt

User="PierreAndre"
#import pylab as plb
home_path=os.getcwd()+os.sep#Current path to this program
sys.path.append(home_path+"src"+os.sep)
data_path=home_path+"Users"+os.sep+User+os.sep+"data"+os.sep#For saving later on data
#print data_path

#########################################################
### Parameters - Sweeping settings
#########################################################
#frequency_start=7033-100 #MHz
#frequency_stop=7033+100 #MHz

Field_min=-10 #gauss
Field_max=110 #gauss
Field_resolution=0.5#0.4 gauss

field_table=np.arange(Field_min,Field_max+Field_resolution,Field_resolution)

number_of_points=field_table.size #Total number of points
Field_step=np.abs(Field_max-Field_min)/number_of_points
print "Scan between %d mT and %d mT at %d mT/min: %d points" %(Field_min,Field_max,Field_step,number_of_points)

test=False #If standalone without anything connected->True
recording={}
recording['Temperature']=False
recording['Field']=True
recording['Lockin-X']=True		#Always True
recording['Lockin-Y']=True		#Always True
recording['Lockin-Name']="Sample 1"
recording['Lockin2-X']=False		#Always True
recording['Lockin2-Y']=False		#Always True
recording['Lockin2-Name']="Sample 2"
recording['Lockin3-X']=False	#Always True
recording['Lockin3-Y']=False		#Always True
recording['Lockin3-Name']="Sample 3"
recording['Field command']=True	#Always True
#########################################################
#########################################################



#########################################################
### Parameters - Data saving
#########################################################
#Calculating the number of columns necessary
number_of_columns=0
for i in recording:
	if recording[i]==True:
		number_of_columns+=1
		
data=Data(given_name="SiP_n2_n1_n6",
		path=data_path,
		comments="bias:1.5V",
		rows=number_of_points,
		columns=number_of_columns,#Field, A channel
		light=7, #V
		temperature=5,#K
		scan_mode="field",
		phase=0,#in degree
		modulation_frequency=4.013,#in kHz
		modulation_amplitude=3,#in V
		receiver_mode=2,
		receiver_gain=1.5, #Or bias EDMR in V
		time_constant=0.3, #in s
		frequency=200,#MHz
		power=0,#dBm
		picture_saving_format='eps',
		user=User)
data.step=0 #Initial value
data.data[:,0]=field_table
#########################################################
#########################################################

#Minimum waiting time = about 300 ms (plot time is limitant)
waiting_time=2*data.time_constant	#s, Waiting time for each point

#########################################################
### Parameters - Magnet
#########################################################
magnet=EDMRmagnet(test)
#########################################################
#########################################################

#########################################################
### Parameters - Gaussmeter
#########################################################
if recording['Field']==True:
	gaussmeter=Lakeshore455("GPIB0::6::INSTR",test)
#########################################################
#########################################################

#########################################################
### Parameters - Lockin
#########################################################
lockin=SR830("GPIB0::7::INSTR",test)
lockin.Reset_Memory()
#lockin.Set_InterExter_Mode(1) #1 for internal mode, 2 for external mode (generated by RF-gen)
lockin.Set_Time_Constant(data.time_constant)
lockin.Set_Harm(data.receiver_mode)  #number of harmonics


if recording['Lockin2-X'] == True:	
	lockin2=SR830("GPIB0::8::INSTR",test)
	lockin2.Reset_Memory()
	#lockin2.Set_InterExter_Mode(2) #1 for internal mode, 2 for external mode (generated by RF-gen)
	lockin2.Set_Time_Constant(data.time_constant)
	lockin2.Set_Harm(data.receiver_mode)  #number of harmonics
	
if recording['Lockin3-X'] == True:	
	lockin3=SR830("GPIB0::9::INSTR",test)
	lockin3.Reset_Memory()
	#lockin3.Set_InterExter_Mode(2) #1 for internal mode, 2 for external mode (generated by RF-gen)
	lockin3.Set_Time_Constant(data.time_constant)
	lockin3.Set_Harm(data.receiver_mode)  #number of harmonics
#lockin.Set_Freq(5000) #modulation frequency in Hz
#########################################################
#########################################################


#########################################################
### Parameters - RF setup
#########################################################
#RF=E8257D("GPIB0::10::INSTR","FM",test)
#RF.Set_Modulation(5232,100)	#Frequency Hz, Modulation deviation kHz / amplitude dB
#RF.Set_Modulation_Output(False,False)	#Modulation On = True, LFOutput On = True
#RF.Set_Power(3)	#dBm
#RF.Set_Frequency(frequency_start)
#RF.Set_Output_ONOFF(True)
#########################################################
#########################################################


#########################################################
### Measure initialization
#########################################################

#Reseting the locking memory
lockin.Reset_Memory()

if recording['Lockin2-X'] == True:	
	lockin2.Reset_Memory()
	
if recording['Lockin3-X'] == True:	
	lockin3.Reset_Memory()

#Start the magnet
magnet.OpenRessource()
magnet.SetMagneticField(data.data[data.step,0], "gauss")
if not test:
	time.sleep(6) #Time to stabilize the magnetic field

internal_time_counter=0

#Preparing the indexes
afterindex = 3
if recording['Lockin2-X'] == True:
	afterindex += 2
if recording['Lockin3-X'] == True:
	afterindex += 2
	
if recording['Field']==True:
	index_Field = afterindex
	index_Temperature = afterindex + 1
elif recording['Field']==False:
	index_Temperature= afterindex

#########################################################
### Measure loop
#########################################################
for i in range(number_of_points):
	
	#Setting the field and wait for stabilization
	magnet.SetMagneticField(data.data[data.step,0], "gauss")
	if not test:
		time.sleep(waiting_time)
	
	#Readings
	X,Y=lockin.Read_XY()
	data.data[data.step,1]=X
	data.data[data.step,2]=Y
	
	if recording['Lockin2-X'] == True:
		X2,Y2=lockin2.Read_XY()
		data.data[data.step,3]=X2
		data.data[data.step,4]=Y2	

	if recording['Lockin3-X'] == True:
		X3,Y3=lockin3.Read_XY()
		data.data[data.step,5]=X3
		data.data[data.step,6]=Y3
	
	#Optional recordings
	if recording['Field']==True:
		data.data[data.step,index_Field]=gaussmeter.Measure_Field()/10000
	if recording['Temperature']==True:
		data.data[data.step,index_Temperature]=thermometer.Measure_Temperature()
	
	#Interfacing
	if recording['Lockin2-X'] == True:
		lockin2print = ", X2 = ", X2, "V, Y2 = ",Y2, "V"
	else:
		lockin2print = ""
		
	if recording['Lockin3-X'] == True:
		lockin3print = ", X3 = ", X3, "V, Y3 = ",Y3, "V"
	else:
		lockin3print = ""
		
	if recording['Field']==True:
		print "Step",data.step,"B0 = ",data.data[data.step,0],"Measured B0 =", data.data[data.step,index_Field], "mT, X = ",X,"V, Y = ",Y,"V",lockin2print,lockin3print
	else:
		print "Step",data.step,"B0 = ",data.data[data.step,0], " G, X = ",X,"V, Y = ",Y,"V",lockin2print,lockin3print
	
	
	#print from time to time the remaining time
	if internal_time_counter==30:
		remaining_time=(number_of_points-(i+1))*waiting_time
		data.Save_data()
		print "Remaining time: ",str(datetime.timedelta(seconds=remaining_time))
		internal_time_counter=1
	
	#increasing counters
	data.step+=1
	internal_time_counter+=1
	#Update field

#########################################################
#########################################################
data.Save_data()
print "Complete."

magnet.CloseRessource()
#########################################################
#########################################################